package com.wejoy.video.middle.station.admin.controller.system;

import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.domain.system.SysUserRole;
import com.wejoy.video.middle.station.data.service.system.ISysUserRoleService;
import com.wejoy.video.middle.station.data.service.system.ISysUserService;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import com.wejoy.video.middle.station.data.vo.system.AddRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统--用户角色关联表
 *
 * @author admin
 * @date 2021年10月19日 15:06:59
 */
@RestController
@RequestMapping("/system/sysUserRole")
public class SysUserRoleController extends BaseController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private ISysUserService sysUserService;


    /**
     * 保存或更新
     *
     * @param sysUserRoleReq sysUserRoleReq
     * @return RespJSON
     */
    @PostMapping("/save")
    public RespJSON save(@RequestBody SysUserRole sysUserRoleReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        sysUserRoleService.save(sysUserRoleReq, loginSysUser.getOperator());
        return RespJSON.success();
    }

    /**
     * 分页查询
     *
     * @param sysUserRoleReq sysUserRoleReq
     * @return RespJSON
     */
    @PostMapping("/getPageList")
    public RespJSON getPageList(@RequestBody SysUserRole sysUserRoleReq) {
        int count = sysUserRoleService.getCount(sysUserRoleReq);
        List<SysUserRole> list = new ArrayList<>();
        if (count > 0) {
            list = sysUserRoleService.getPageList(sysUserRoleReq);
        }
        PageResult result = new PageResult(sysUserRoleReq.getPageNo(), sysUserRoleReq.getPageSize(), count, list);
        return RespJSON.success(result);
    }

    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    public RespJSON getOneById(Long id) {
        return RespJSON.success(sysUserRoleService.getOneById(id));
    }

    /**
     * 为用户分配角色
     *
     * @param addRoleVO
     * @param loginSysUser
     * @return
     */
    @PostMapping("/addRole")
    @RoleCheck(name = "sys_user_addRole")
    public RespJSON addRole(@RequestBody AddRoleVO addRoleVO, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        sysUserRoleService.addRole(addRoleVO, loginSysUser);
        return RespJSON.success();
    }

    /**
     * 列表查询
     *
     * @param sysUserRoleReq
     * @param loginSysUser
     * @return
     */
    @PostMapping("/getList")
    public RespJSON addRole(@RequestBody SysUserRole sysUserRoleReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        List<SysUser> sysUserList = sysUserRoleService.getList(sysUserRoleReq).stream().map(item -> {
            return sysUserService.getExitOneById(item.getSysUserId());
        }).collect(Collectors.toList());
        sysUserList.removeAll(Collections.singleton(null));
        return RespJSON.success(sysUserList);
    }

}
